import { Component, Node, find, director } from 'cc';

export class SingletonComponent extends Component {

    public static getInstance<T extends SingletonComponent>(this: (new () => T) | typeof SingletonComponent): T {
        let globalNode:Node=find("GlobalGameNode")
        if(!globalNode){
            globalNode=new Node("GlobalGameNode")
            director.addPersistRootNode(globalNode)
        }
        let comp:any=globalNode.getComponent(this)
        if(!comp){
            comp=globalNode.addComponent(this)
            comp.init()
        }
        return comp
    }

    protected init(){
        //
    }
}


